﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rabbit.Domain;
using Rabbit.DTO.Input;
using Rabbit.DTO.Output;
using Rabbit.Service;

namespace Rabbit.WebApi.Controllers
{
    /// <summary>
    /// 溯源管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceManageController : ControllerBase
    {

        TraceManageService res;

        public TraceManageController(TraceManageService res)
        {
            this.res = res;
        }

        /// <summary>
        /// 商品显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTraceManages()
        {
            return Ok(res.GetTraceManages());
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetImage(IFormFile file)
        {
            string fileName = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileName).ToLower();
            string path = Directory.GetCurrentDirectory() + @"\wwwroot\Image\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //// 使用固定的 IP 地址替换 localhost
            //string ipAddress = "182.92.209.50"; // 替换为你的 IP 地址
            //string returnurl = $"{Request.Scheme}://{ipAddress}/Image/{fileName}";
            string returnurl = $"{Request.Scheme}://{Request.Host}"+"/Image/" + fileName;
            return Ok(returnurl);
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTraceManage(TraceManageInput input)
        {
            return Ok(res.AddTraceManage(input));
        }

        /// <summary>
        /// 删除（逻辑删）
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelT(string ids,string names)
        {
            return Ok(res.DelT(ids,names));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdT(TraceManage entity)
        {
            return Ok(res.UpdT(entity));
        }

        /// <summary>
        /// 修改导出数量
        /// </summary>
        /// <param name="number"></param>
        /// <param name="tMid"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetDc(int number, int tMid)
        {
            return Ok(res.GetDc(number, tMid));
        }

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TraceManageInfo(int tmid)
        {
            return Ok(res.TraceManageInfo(tmid));
        }





        }
}
